package by.bucha.testproject.fragments;

import com.actionbarsherlock.app.SherlockListFragment;

import by.bucha.tabnavigationproject.R;
import by.bucha.tabnavigationproject.tabsframework.AbstractTabActivity;
import by.bucha.testproject.adapters.ParsedXmlListAdapter;
import by.bucha.testproject.content.ContentManager;
import by.bucha.testproject.response.CallbackAsyncTask;
import by.bucha.testproject.xml.bo.ResultModel;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class ParsedXmlListFragment extends SherlockListFragment {

	public AbstractTabActivity getTabStack() {
		return (AbstractTabActivity) getActivity();
	}

	private ParsedXmlListAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(AbstractTabActivity.TAG, "onCreateView in fragment "
				+ getClass().getName());

		mAdapter = new ParsedXmlListAdapter(getActivity()
				.getApplicationContext(),
				R.layout.view_fragment_parsed_xml_list, null);
		this.setListAdapter(mAdapter);
		ContentManager.getInstance().getResponse(
				new CallbackAsyncTask<ResultModel>() {

					@Override
					public void onSuccess(ResultModel obj) {
						if (obj != null && obj.getListQuoteLength() != 0) {
							mAdapter.changeData(obj.getListQuote());
							getTabStack().refreshFragmentView(
									ParsedXmlListFragment.this);
						} else {
							String warning = getActivity()
									.getApplicationContext()
									.getResources()
									.getString(
											R.string.parsed_xml_list_fragment_strings_no_data);

							Toast.makeText(
									getActivity().getApplicationContext(),
									warning, Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onError(Exception e) {
						String warning = getActivity()
								.getApplicationContext()
								.getResources()
								.getString(
										R.string.parsed_xml_list_fragment_strings_exception);

						Toast.makeText(getActivity().getApplicationContext(),
								warning + ": " + e.getMessage(),
								Toast.LENGTH_SHORT).show();
					}
				});
		return super.onCreateView(inflater, container, savedInstanceState);
	}
}
